package com.is.add.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.is.add.controller.vo.DepositBalanceVO;
import com.is.add.controller.vo.DepositPageListVO;
import com.is.add.controller.vo.DepositRecordVO;
import com.is.add.enmus.DepositOptTypeEnum;
import com.is.add.mapper.PreDepositItemMapper;
import com.is.add.model.Deposit;
import com.is.add.service.DepositService;
import com.is.add.util.DateUtil.DateStyle;
import com.is.add.util.ExcelUtil.ExcelUtil;
import com.is.add.util.ExcelUtil.GetPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepositServiceImpl implements DepositService{
    @Autowired
    private PreDepositItemMapper depositItemMapper;
    private static final String FILE_SEPARATOR = System.getProperties().getProperty("file.separator");
    private static String excelPath = "/home/settlePT/excel/";
    /**
     * 备用金页选择项
     * @return  键值对list
     */
    public DepositPageListVO getDepositPageList(){
        return new DepositPageListVO(DepositOptTypeEnum.toList());
    }

    /**
     * 备用金流水记录查询
     */
    @Override
    public PageInfo<DepositRecordVO> depositRechargeRecordQuery(Map<String, Object> map, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Deposit> depositList = this.depositItemMapper.selectRechargeRecord(map);
        PageInfo<Deposit> pageInfo = new PageInfo<>(depositList);
        PageInfo<DepositRecordVO> list = new PageInfo<>(DepositRecordVO.generateBy(pageInfo.getList()));
        list.setPageNum(pageInfo.getPageNum());
        list.setPageSize(pageInfo.getPageSize());
        list.setSize(pageInfo.getSize());
        list.setOrderBy(pageInfo.getOrderBy());
        list.setStartRow(pageInfo.getStartRow());
        list.setEndRow(pageInfo.getEndRow());
        list.setTotal(pageInfo.getTotal());
        list.setPages(pageInfo.getPages());
        list.setFirstPage(pageInfo.getFirstPage());
        list.setPrePage(pageInfo.getPrePage());
        list.setNextPage(pageInfo.getNextPage());
        list.setLastPage(pageInfo.getLastPage());
        list.setIsFirstPage(pageInfo.isIsFirstPage());
        list.setIsLastPage(pageInfo.isIsLastPage());
        list.setHasPreviousPage(pageInfo.isHasPreviousPage());
        list.setHasNextPage(pageInfo.isHasNextPage());
        list.setNavigatePages(pageInfo.getNavigatePages());
        list.setNavigatepageNums(pageInfo.getNavigatepageNums());
        return list;
    }
    /**
     * 备用金余额查询
     */
    @Override
    public PageInfo<DepositBalanceVO> balanceQuery(Map<String, Object> map, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Deposit> depositList = this.depositItemMapper.selectDepositBalance(map);
        PageInfo<Deposit> pageInfo = new PageInfo<>(depositList);
        PageInfo<DepositBalanceVO> list = new PageInfo<>(DepositBalanceVO.generateBy(pageInfo.getList()));
        list.setPageNum(pageInfo.getPageNum());
        list.setPageSize(pageInfo.getPageSize());
        list.setSize(pageInfo.getSize());
        list.setOrderBy(pageInfo.getOrderBy());
        list.setStartRow(pageInfo.getStartRow());
        list.setEndRow(pageInfo.getEndRow());
        list.setTotal(pageInfo.getTotal());
        list.setPages(pageInfo.getPages());
        list.setFirstPage(pageInfo.getFirstPage());
        list.setPrePage(pageInfo.getPrePage());
        list.setNextPage(pageInfo.getNextPage());
        list.setLastPage(pageInfo.getLastPage());
        list.setIsFirstPage(pageInfo.isIsFirstPage());
        list.setIsLastPage(pageInfo.isIsLastPage());
        list.setHasPreviousPage(pageInfo.isHasPreviousPage());
        list.setHasNextPage(pageInfo.isHasNextPage());
        list.setNavigatePages(pageInfo.getNavigatePages());
        list.setNavigatepageNums(pageInfo.getNavigatepageNums());

        return list;
    }

    /**
     * 导出备用金余额
     */
    @Override
    public void exportDepositBalance(HttpServletResponse response, String endTime, Long depositType) {
        Map<String,Object> map =new HashMap<>();
        if (depositType != null){
            map.put("depositType",depositType);
        }
        map.put("endTime",endTime);
        List<Deposit> depositList = this.depositItemMapper.selectDepositBalance(map);
        //设置路径
        String docsPath = GetPath.getDirectoryPath("src","main","resources","templates");       //模板文件路径
        String fileName = endTime+"备用金余额表"+".xls";           //导出Excel文件名
        String filePath = docsPath + FILE_SEPARATOR + fileName;

        filePath = excelPath + fileName;
        //数据填充
        ExcelUtil<DepositBalanceVO> ex = new ExcelUtil<DepositBalanceVO>();
        String[] headers ={"场馆Id","场馆编号","场馆名称","余额","备用金类型"};
        String[] columns={ "shopId", "shopCode", "shopName","balance","depositType"};
        try {
            OutputStream out = new FileOutputStream(filePath);
            ex.exportExcel("备用金余额表"+endTime,headers, columns, DepositBalanceVO.generateBy(depositList), out, DateStyle.YYYY_MM_DD_HH_MM_EN.getValue());
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.err.println("文件未找到");
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("IO异常错误");
        } catch (Exception e) {
            e.printStackTrace();
        }
        ExcelUtil.download(filePath, response);
    }

    /**
     * 导出备用金流水
     */
    @Override
    public void exportDepositRechargeRecord(HttpServletResponse response, String startTime, String endTime, DepositOptTypeEnum optType) {
        Map<String,Object> map =new HashMap<>();
        if (optType != null){
            map.put("optType",optType.getText());
        }
        map.put("startTime",startTime+" 00:00:00");
        map.put("endTime",endTime + " 23:59:59");
        List<Deposit> depositList = this.depositItemMapper.selectRechargeRecord(map);
        //设置路径
        String docsPath = GetPath.getDirectoryPath("src","main","resources","templates");       //模板文件路径
        String fileName = "备用金流水记录"+startTime+"-"+endTime+".xls";           //导出Excel文件名
        String filePath = docsPath + FILE_SEPARATOR + fileName;

        filePath = excelPath + fileName;
        //数据填充
        ExcelUtil<DepositRecordVO> ex = new ExcelUtil<DepositRecordVO>();
        String[] headers ={"场馆Id","场馆编号","场馆名称","原始金额","变动金额","操作后金额","操作","操作时间","备注"};
        String[] columns={ "shopId", "shopCode", "shopName","oldSum","changeSum","newSum","otpType","optTime","remark"};
        try {
            OutputStream out = new FileOutputStream(filePath);
            ex.exportExcel(startTime+"-"+endTime+"备用金流水记录",headers, columns, DepositRecordVO.generateBy(depositList), out, DateStyle.YYYY_MM_DD_HH_MM_EN.getValue());
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.err.println("文件未找到");
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("IO异常错误");
        } catch (Exception e) {
            e.printStackTrace();
        }
        ExcelUtil.download(filePath, response);
    }
}
